package com.wmz.picture.manager;

import cn.hutool.core.io.FileUtil;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.model.COSObject;
import com.qcloud.cos.model.GetObjectRequest;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.model.ciModel.persistence.PicOperations;
import com.wmz.picture.config.CosClientConfig;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lihechun
 * @version 1.0
 * @description: 对象存储管理类
 * @date: 2024/12/16 22:55
 */
@Component
public class CosManager {

    @Resource
    private CosClientConfig cosClientConfig;

    @Resource
    private COSClient cosClient;

    /**
     * 上传对象
     *
     * @param key  唯一键
     * @param file 文件
     */
    public PutObjectResult putObject(String key, File file) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), key,
                file);
        return cosClient.putObject(putObjectRequest);
    }
    /**
     * 下载对象
     *
     * @param key 唯一键
     */
    public COSObject getObject(String key) {
        GetObjectRequest getObjectRequest = new GetObjectRequest(cosClientConfig.getBucket(), key);
        return cosClient.getObject(getObjectRequest);
    }
    /**
     * 上传图片持久化
     *
     * @param key  唯一键
     * @param file 文件
     */
    public PutObjectResult putPictureObject(String key, File file) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), key, file);
        // 设置图片信息
        PicOperations picOperations = new PicOperations();
        // 1.表示返回原图信息
        picOperations.setIsPicInfo(1);
        List<PicOperations.Rule> rules = new ArrayList<>();
        // 设置图片新格式
        String webKey = FileUtil.mainName(key) + ".webp";
        // 图片压缩（转成webp格式)
        PicOperations.Rule compressRule = new PicOperations.Rule();
        compressRule.setBucket(cosClientConfig.getBucket());
        compressRule.setFileId(webKey);
        compressRule.setRule("imageMogr2/format/webp");
        rules.add(compressRule);
        // 仅对大于20KB的图片进行压缩
        if(file.length() > 20 * 1024) {
            // 缩略图名称
            String thumbnailKey = FileUtil.mainName(key) + "_thumbnal." + FileUtil.getSuffix(key);
            // 缩略图处理
            PicOperations.Rule thumbnalRule = new PicOperations.Rule();
            thumbnalRule.setBucket(cosClientConfig.getBucket());
            thumbnalRule.setFileId(thumbnailKey);
            // 设置缩放规则/thumbnail/<width>x<height>>（如果大于原图宽高，则不处理）
            thumbnalRule.setRule(String.format("imageMogr2/thumbnail/%sx%s>",128,128));
            rules.add(thumbnalRule);
        }
        // 构造处理函数
        picOperations.setRules(rules);
        putObjectRequest.setPicOperations(picOperations);
        return cosClient.putObject(putObjectRequest);
    }

    /**
     * 删除对象
     *
     * @param key 文件 key
     */
    public void deleteObject(String key) throws CosClientException {
        cosClient.deleteObject(cosClientConfig.getBucket(), key);
    }


}
